//index.js
//获取应用实例
const app = getApp()
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');
Page({
	data: {
		windowWidth: app.systemInfo.windowWidth,
		windowHeight: app.systemInfo.windowHeight,
		pixelRatio: app.systemInfo.pixelRatio,
		currentTab: 0,
		orderList:[],
		pageNum:1,//当前页
		total:0,//总条数
		customerId:"",
		orderStatus:"3"
	},
	// 点击tab切换 
	statusFind: function(e) {
		if(e.target.dataset.current == "0") {
			this.setData({
				orderStatus: "3"
			})
		} else {
			this.setData({
				orderStatus: "2"
			})
		}
		this.setData({
			currentTab: e.target.dataset.current,
			pageNum:1,
			orderList:[],
		})
		this.getOrderList();
	},
	onShow: function() {
		let _this = this;
		wx.getStorage({
		 	key: 'customerId',
		  	success: function(res) {
		  		_this.setData({
					customerId: res.data,
				});
		      	_this.getOrderList();
		  	} 
		})
	},
	getOrderList:function(){
		let _this = this;
		api.getOrderList({
			data: {
				orderStatus: _this.data.orderStatus,
				customerId: _this.data.customerId,
				pageSize: "20",
				pageNum: _this.data.pageNum,
			},
			success: function(res) {
				let array = _this.data.orderList;
				res.data.obj.rows.forEach(function(item) {
					if(item.courseTime.startTime != null) {
						item.courseTime.startTime = util.formatEndTime(new Date(item.courseTime.startTime));
					}
					if(item.orderStatus == "0") {
						item.orderStatusName = "待支付";
					} else if(item.orderStatus == "1") {
						item.orderStatusName = "待核销";
					} else if(item.orderStatus == "2") {
						item.orderStatusName = "已取消";
					}else if(item.orderStatus == "-1") {
						item.orderStatusName = "已退款";
					}else {
						item.orderStatusName = "已核销";
					}
					array.push(item);
				});
				if(res.data.code == 0) {
					_this.setData({
						orderList: array,
						total:res.data.obj.total
					});
				}
			},
		});
	},
	loadMore:function(){
		let _this = this;
		console.log(this.data.total);
		if(this.data.total > this.data.pageNum * 20){
			setTimeout(function(){
				_this.setData({
					pageNum: _this.data.pageNum + 1
				});
				_this.getOrderList();
			},500);
		}else{
			console.log(this.data.pageNum)
		}
	}

})